#include <stdio.h>

#include "debug.h"
#include "esp_task.h"
#include "doorbell_sound.h"
#include "doorbell_wifi.h"
#include "doorbell_mqtt.h"

#include "doorbell_ws.h"
#include "doorbell_video.h"
#include "doorbell_led.h"

uint8_t data[1024];

void net_succ_cb(void)
{
    MY_LOGI("wifi 连接成功");

    /* 1. 初始化mqtt客户端 */
    doorbell_mqtt_Init();

    /* 2. 初始化ws客户端 */
    doorbell_ws_Init();
}

void net_fail_cb(void)
{
    MY_LOGI("wifi 连接失败");
}

TaskHandle_t mainTask;
void         app_main(void)
{
    MY_LOGI("尚硅谷智能门铃项目");

    mainTask = xTaskGetCurrentTaskHandle();

    // MY_LOGI("1 %d",uxTaskPriorityGet(xTaskGetCurrentTaskHandle()));
    vTaskPrioritySet(mainTask, 10);

    /* 1.初始化音频设备和打开音频设备 */
    doorbell_sound_Init();
    doorbell_sound_Open();

    /* 2. 初始化视频 */
    doorbell_video_Init();

    /* 3. 初始化led */
    doorbell_led_Init();


    /* 3. wifi的初始化 */
    doorbell_wifi_Init();
    doorbell_wifi_Start(net_succ_cb, net_fail_cb);
    //

    uint32_t cmd = 0;
    while(1)
    {
        xTaskNotifyWait(0, 0, &cmd, portMAX_DELAY);
        if(cmd == on_audio_client2esp)
        {
            // unsafely-treat-insecure-origin-as-secure
            MY_LOGE("打开 音频 主人->门铃");
            doorbell_ws_audio_open(DIR_CLIENT2ESP);
        }
        else if(cmd == off_audio_client2esp)
        {
            MY_LOGE("关闭 音频 主人->门铃");
            doorbell_ws_audio_close(DIR_CLIENT2ESP);
        }
        else if(cmd == on_audio_esp2client)
        {
            MY_LOGE("打开 音频 门铃->主人");
            doorbell_ws_audio_open(DIR_ESP2CLIENT);
        }
        else if(cmd == off_audio_esp2client)
        {
            MY_LOGE("关闭 音频 门铃->主人");
            doorbell_ws_audio_close(DIR_ESP2CLIENT);
        }
        else if(cmd == on_video_esp2client)
        {
            MY_LOGE("打开 视频 门铃->主人");
            doorbell_led_On();
            doorbell_ws_video_open(DIR_ESP2CLIENT);
        }
        else if(cmd == off_video_esp2client)
        {
            MY_LOGE("关闭 视频 门铃->主人");
            doorbell_ws_video_close(DIR_ESP2CLIENT);
            doorbell_led_Off();
        }
    }
}
/*
https://www.emqx.com/zh/mqtt/public-mqtt5-broker
https://mqttx.app/web-client/#/recent_connections/94e0e9d0-323b-4d6f-84ce-e093b7a84d14

*/
